using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;

using PlugNT.Safe;
using PlugNT.Common;
using PlugNT.Cms.Custom;
using PlugNT.Cms.Model;

namespace PlugNT.Cms.BLL
{



    public class Category
    {

        private static readonly PlugNT.Cms.DAL.Category dal = new PlugNT.Cms.DAL.Category();









        public CategoryInfo GetCategoryInfo(int id)
        {
            return dal.GetCategoryInfo(id);

        }


        public string GetNoList(int id)
        {
            return dal.GetNoList(id);
        }

        public string GetHvalue(int id)
        {
            return dal.GetHvalue(id);
        }







        public bool Add(CategoryInfo model)
        {
            return dal.Add(model);
        }







        public bool Update(CategoryInfo model, bool isSubModel)
        {
            return dal.Update(model,isSubModel);
        }






        public bool Delete(int id)
        {
            return dal.Delete(id);
        }








        public bool ExistsParent(int id)
        {
            return dal.ExistsParent(id);
        }






        public static void SetCacheJson(int id)
        {
            string cachefile =Utils.GetMapPath("~/Logs/Cache/Category_" + id + ".js");
            bool first = true;

            DataTable dt = dal.GetOrderList(dal.GetNoList(id));
            StringBuilder sb = new StringBuilder();
            sb.Append("var category_" + id + "=[");
            foreach (DataRow dr in dt.Rows)
            {
                string layer = dr["no_list_len"].ToString();
                string text = dr["name"].ToString();
                string value = (dr["is_h_value"].ToString() == "1") ? dr["h_value"].ToString() : dr["no_list"].ToString();
                sb.Append((first ? "" : ",") + "{\"text\":\"" + text + "\",\"value\":\"" + value + "\",\"layer\":\"" + layer + "\"}");
                if (first) { first = false; }
            }
            sb.Append("]");
            Utils.CreateFile(cachefile, sb.ToString(), System.Text.Encoding.GetEncoding("UTF-8"));
        }






        public string GetCacheJson(int id)
        {
            string cachefile = Utils.GetMapPath("~/Logs/Cache/Category_" + id + ".js");
            if (!Utils.FileExists(cachefile))
            {
                SetCacheJson(id);
            }
            return Utils.ReadFileToEnd(cachefile, System.Text.Encoding.GetEncoding("UTF-8"));
        }



  





        public DataTable GetOrderList(string noList)
        {
            return dal.GetOrderList(noList);
        }






        public DataTable GetNolistList(string noList)
        {
            return dal.GetNolistList(noList);
        }







        public DataTable GetSubList(int id, bool incCurrent)
        {
            return dal.GetSubList(id, incCurrent);
        }






        public DataTable GetSubList(string noList, bool incCurrent)
        {
            return dal.GetSubList(noList, incCurrent);
        }






        public DataTable GetListRow(int id)
        {
            return dal.GetListRow(id);

        }



    }

}

